package promote.brozen.dubbo.providers.javaconfig;

import org.apache.dubbo.config.*;
import org.apache.dubbo.config.bootstrap.builders.ProviderBuilder;
import org.apache.dubbo.config.spring.ServiceBean;
import promote.brozen.dubbo.interfaces.api.UserApi;
import promote.brozen.dubbo.providers.api.UserApiProvider;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.LockSupport;

/**
 * @author Brozen
 * @date 2020/9/9 9:24 AM
 * @email brozen@qq.com
 */
public class JavaExporter {

    public static void main(String[] args) {
        new JavaExporter().exportUserApi();
        LockSupport.park();
    }

    private ApplicationConfig applicationConfig;

    private RegistryConfig registryConfig;

    private ProtocolConfig protocolConfig;

    private ProviderConfig providerConfig;

    private Map<String, ServiceConfig<?>> services;

    public JavaExporter() {

        // Application的配置可以参考 http://dubbo.apache.org/zh-cn/docs/user/references/xml/dubbo-application.html
        applicationConfig = new ApplicationConfig();
        applicationConfig.setName("TestExportApiWithJavaConfig");

        // http://dubbo.apache.org/zh-cn/docs/user/references/xml/dubbo-registry.html
        registryConfig = new RegistryConfig();
        registryConfig.setAddress("nacos://127.0.0.1:8848?namespace=484a0ac6-add3-4ea8-847d-cdefec40f5b8");
        registryConfig.setProtocol("nacos");
        registryConfig.setGroup("dev");

        // 服务提供者协议，http://dubbo.apache.org/zh-cn/docs/user/references/xml/dubbo-protocol.html
        protocolConfig = new ProtocolConfig();
        protocolConfig.setName("dubbo");
        protocolConfig.setPort(30010);
        protocolConfig.setThreads(4);

        // Provider的配置
        providerConfig = new ProviderBuilder()
                .application(applicationConfig)
                .addRegistry(registryConfig)
                .addProtocol(protocolConfig)
                .build();

        services = new ConcurrentHashMap<>();
    }

    private void exportUserApi() {


        // 暴露服务提供者
        ServiceConfig<UserApi> userService = new ServiceConfig<>();
        userService.setProvider(providerConfig);
        userService.setInterface(UserApi.class);
        userService.setRef(new UserApiProvider());
        userService.setVersion("0.0.1");
        userService.setGroup("brozen");
        userService.export();
        services.put("userApi", userService);

    }

}
